package fr.gla.hibtracker.historique.bm;


/**
 * Historique d'une modification sur une valeur d'un champ.
 *
 * @see GroupeModifications
 * @see FlushModifications
 *
 */
public class Modification {

    /**
     * Identifiant composite.
     */
    private ModificationID modificationID = new ModificationID();

    private String         oldValue;

    private String         newValue;



    public Modification() {
        super();
    }

    public Modification(ModificationID modificationID, String oldValue, String newValue) {
        super();
        this.modificationID = modificationID;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    public String getNewValue() {
        return newValue;
    }

    public void setNewValue(String newValue) {
        this.newValue = newValue;
    }

    public String getOldValue() {
        return oldValue;
    }

    public void setOldValue(String oldValue) {
        this.oldValue = oldValue;
    }

    public ModificationID getModificationID() {
        return modificationID;
    }

    public void setModificationID(ModificationID modificationID) {
        this.modificationID = modificationID;
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        if (modificationID != null && modificationID.getConfigModification() != null
                        && modificationID.getConfigModification().getId() != null) {
            builder.append(" [el = ")
                   .append(modificationID.getConfigModification().getId().getEl())
                   .append("]")
                   .append("\n");
        }
        boolean oldValueContainsCarretReturn = getOldValue() != null && (getOldValue().contains("\n"));
        boolean newValueContainsCarretReturn = getNewValue() != null && (getNewValue().contains("\n"));
        builder.append(" oldValue = ")
               .append((getOldValue().length() != 0 && oldValueContainsCarretReturn) ? "\n" : "")
               .append(getOldValue())
               .append("\n newValue = ")
               .append((getNewValue().length() != 0 && newValueContainsCarretReturn) ? "\n" : "")
               .append(getNewValue());
        return builder.toString();
    }
}
